package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增套餐
     * @param setmeal
     * @return
     */
    public void insert(SetmealDTO setmeal) {
        Setmeal entity = new Setmeal();
        //为创建 套餐的作者赋值
        entity.setCreateUser(BaseContext.getCurrentId());
        entity.setUpdateUser(BaseContext.getCurrentId());
        BeanUtils.copyProperties(setmeal, entity);
        //套餐表 插入数据
         setmealMapper.insert(entity);
        //获取生成的套餐id
        Long setmealId = setmeal.getId();
         //套餐表和菜品表关联数据的插入
        List<SetmealDish> list = setmeal.getSetmealDishes();
        list.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(list);

    }

    /**
     * 套餐关联分类分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);//后绪步骤实现
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 更新套餐起售、停售
     * @param status
     * @return
     */
    public void updateSetmealStatus(Integer status,Long id) {
        Setmeal entity = Setmeal.builder()
                        .status(status)
                        .id(id)
                        .build();
        setmealMapper.update(entity);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    public void deleteSetmeal(List<Long> ids) {
        //先判断套餐的状态 如果为停售则删除
        ids.forEach(id -> {
            Setmeal setmael = setmealMapper.getById(id);
            //如果套餐的状态为1 则可以删除
            if (setmael.getStatus()== StatusConstant.DISABLE) {
                setmealMapper.deleteById(id);
                //删除完套餐以后再删除套餐和菜品关联表中的数据
                setmealDishMapper.deleteBySetmealId(id);
            }else if(setmael.getStatus()== StatusConstant.ENABLE){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

        });

    }
    /**
     * 根据id查询套餐 并且关联菜品
     *
     * @param id
     * @return
     */
    public SetmealVO getSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
    }


}
